package client;
import java.util.ArrayList;

import msgs.*;
import msgs.TTConstants.Actions;
import msgs.TTConstants.Responses;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * TigerTalk Chat thread/window/thing
 * Team 9: Andrew Hayworth, Brian Parrella, Ryan Kortmann, Nina Papa
 * @author Andrew Hayworth
 */

public class Chat {
	private JFrame jf;
	private String title;
	
	// static list of who is an admin, who is a member.
	// admin isnt really implemented yet
	private static ArrayList<String> admins, members;
	private boolean closed;
	private JTextField msgEntry;
	private JTextArea msgsDisplay;
	//private JList membersList;
	private JButton send;
	private JScrollPane jsp;
	private JPanel jp;
	private ChatMsg c;
	
	// my gui code makes my eyes bleed...
	public Chat(ChatMsg c) {
		jf = new JFrame();
		this.c = c;
		
		title = c.getTitle();
		admins = c.getAdmins();
		members = c.getMembers();
		
		closed = c.isClosed();
		
		//TODO clean me up! Use BoxLayout!
		jp = new JPanel(new BorderLayout());
		
		msgsDisplay = new JTextArea();
		msgsDisplay.setEditable(false);
		jsp = new JScrollPane(msgsDisplay);
		jf.add(jsp);
		
		String[] mbm = {};
		mbm = members.toArray(mbm);
		//membersList = new JList(mbm);
		
		msgEntry = new JTextField();
		send = new JButton("Send");
		send.setMnemonic('S');
		jp.add(msgEntry);
		jp.add(send, BorderLayout.EAST);
				
		jf.add(jp, BorderLayout.SOUTH);
		jf.setSize(300,300);
		jf.setTitle("Group Chat: " + title);
		jf.setLocationRelativeTo(null);
		jf.setVisible(true);
		
		send.addActionListener(new Sender());
		msgEntry.requestFocusInWindow();
		
		jf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				ChatMsg cm = new ChatMsg(TTClient.ip, TTClient.name, TTClient.pwd, TTClient.key, null, null, title, false);
				// let the server know we're leaving.
				cm.setAction(Actions.LEAVE_CHAT);
				ServerSender.outQ.add(cm);
				// pull us out of the local db.
				TTClient.chats.remove(title);
				jf.dispose();
			}
		});
	}
	
	// Internal class for sending updates to the window
	class Sender implements ActionListener {
		public void actionPerformed(ActionEvent ae ) {
			// create a ChatMsg to let the server know we're updating.
			c.setAction(Actions.CHAT_UPDATE);
			c.setMsg(msgEntry.getText());
			c.setTitle(title);
			ServerSender.outQ.add((ClientMsg)c);
			TTClient.playMsgOut();
		}
	}
	
	/**
	 * Updates that chatwindow ONLY
	 * @param who Who's it from?
	 * @param msg What did they say?
	 */
	public void updateChat(String who, String msg) {
		System.out.println("Update Chat: " + who + " : " + msg);
		msgsDisplay.append(who + ": " + msg + "\n");
	}
	
	////////////
	//BEGIN MASSIVE LIST OF GETTERS AND SETTERS
	//THANK YOU ECLIPSE!
	////////////
	public ArrayList<String> getAdmins() {
		return c.getAdmins();
	}

	public ArrayList<String> getMembers() {
		return c.getMembers();
	}

	public String getMsg() {
		return c.getMsg();
	}

	public String getTitle() {
		return c.getTitle();
	}

	public boolean isClosed() {
		return c.isClosed();
	}

	public void setAdmins(ArrayList<String> admins) {
		c.setAdmins(admins);
	}

	public void setClosed(boolean closed) {
		c.setClosed(closed);
	}

	public void setMembers(ArrayList<String> members) {
		c.setMembers(members);
	}

	public void setMsg(String msg) {
		c.setMsg(msg);
	}

	public void setTitle(String title) {
		c.setTitle(title);
	}

	public Actions getAction() {
		return c.getAction();
	}

	public String[] getChats() {
		return c.getChats();
	}

	public byte[] getClientKey() {
		return c.getClientKey();
	}

	public String getIP() {
		return c.getIP();
	}

	public String getIp() {
		return c.getIp();
	}

	public String getName() {
		return c.getName();
	}

	public String getPwd() {
		return c.getPwd();
	}

	public Responses getResponse() {
		return c.getResponse();
	}

	public String getUser() {
		return c.getUser();
	}

	public boolean isLogIt() {
		return c.isLogIt();
	}

	public void setAction(Actions action) {
		c.setAction(action);
	}

	public void setChats(String[] chats) {
		c.setChats(chats);
	}

	public void setClientKey(byte[] k) {
		c.setClientKey(k);
	}

	public void setIP(String ip) {
		c.setIP(ip);
	}

	public void setIp(String ip) {
		c.setIp(ip);
	}

	public void setLogIt(boolean logIt) {
		c.setLogIt(logIt);
	}

	public void setName(String n) {
		c.setName(n);
	}

	public void setPwd(String p) {
		c.setPwd(p);
	}

	public void setResponse(Responses response) {
		c.setResponse(response);
	}

	public void setUser(String user) {
		c.setUser(user);
	}

	public boolean equals(Object obj) {
		return c.equals(obj);
	}

	public int hashCode() {
		return c.hashCode();
	}

	public String toString() {
		return c.toString();
	}
	
	
	
}
